<?php
/**
 * Created by PhpStorm.
 * User: adminyj
 * Date: 2019/2/17
 * Time: 20:17
 */

namespace app\admin\controller;


use app\common\controller\Adminbase;
use think\Db;
use PHPExcel;
use think\Request;

class Work extends Adminbase
{

    /**
     * 创业内容信心修改
     * */
    public function config()
    {

        if($this->request->isPost()){

            $data = input('post.');

            $res = Db::name('work_config')->where(['id' => 1])->update($data);

            if($res){
                $this->success('操作成功');
            }else{
                $this->error('操作失败');
            }

        }else{
            $httpType = is_https();

            if($httpType){
                $https = "https://";
            }else{
                $https = "http://";
            }

            $http = $_SERVER['SERVER_NAME'];
            $data = Db::name('work_config')->where(['id' => 1])->find();

            $data['imgs1'] = $https.$http."/uploads/images/work/".$data['img1'];
            $data['imgs2'] = $https.$http."/uploads/images/work/".$data['img2'];
            $data['imgs3'] = $https.$http."/uploads/images/work/".$data['img3'];

            $this->assign('data',$data);
            return $this->fetch();
        }

    }


    /**
     * 人力资源市场管理列表
     * */
    public function index()
    {

        if($this->request->isAjax()){

            $limit = $this->request->param('limit/d', 10);
            $page = $this->request->param('page/d', 10);

            $where[] = ['type','=','1'];
            $where[] = ['status','=','1'];

            $phone = input('phone');
            if($phone){
                $where[] = ['phone','=',$phone];
            }
            $title = input('title');
            if($title){
                $where[] = ['title','like','%'.$title.'%'];
            }
            $company = input('company');
            if($company){
                $where[] = ['company','like','%'.$company.'%'];
            }
            $time = input('time');
            if($time){
                $start_time = substr($time, 0, 20);//订单那创建开始时间
                $end_time = substr($time, 21, 20);//订单那创建开始时间
                $time = strtotime($start_time);
                $times = strtotime($end_time);
                $where[] = ['create_time','between',$time.','.$times];
            }

            $order = "id DESC";
            $list = Db::name('work_list')->where($where)
                ->page($page,$limit)
                ->order($order)
                ->withAttr('create_time', function ($value, $data) {
                    if($value){
                        return date('Y-m-d H:i:s', $value);
                    }else{
                        return "--";
                    }
                })
                ->select();
            $total = Db::name('work_list')->where($where)->count();

            $result = array("code" => 0, "count" => $total, "data" => $list);
            return json($result);

        }
        return $this->fetch();

    }


    /**
     * 工作内容编辑/添加
     * */
    public function edit()
    {

        if($this->request->isPost()){

            $data = input('post.');
            if(!empty($data['workId'])){
                $update = 1;
                $id = $data['workId'];
            }else{
                $update = 0;
            }
            unset($data['workId']);

            $data['status'] = 1;
            if($update == 1){
                $res = Db::name('work_list')->where(['id' => $id])->update($data);
            }else{
                $res = Db::name('work_list')->insert($data);
            }

            if($res){
                $this->success('操作成功');
            }else{
                $this->error('操作失败');
            }

        }else{
            $id = $this->request->param('id/d');

            if($id){
                $data = Db::name('work_list')->where(['id' => $id])->find();
            }else{
                $data = [
                    'id' => '',
                    'title' => '',
                    'wages' => '',
                    'human_num' => '',
                    'age' => '',
                    'sex' => '',
                    'education' => '',
                    'condition' => '',
                    'company' => '',
                    'contacts' => '',
                    'phone' => '',
                    'address' => '',
                    'sort' => '',
                    'type' => '',
                    'info' => ''
                ];
            }
            $this->assign('data',$data);

            return $this->fetch();
        }

    }


    /**
     * 工作信息删除
     * */
    public function del()
    {

        $id = input('id');
        if(!$id){
            $this->error('数据获取有误');
        }
        $data['status'] = -1;
        $res = Db::name('work_list')->where(['id' => $id])->update($data);

        if($res){
            $this->success('删除成功');
        }else{
            $this->error('删除失败');
        }

    }

    /**
     * 其他岗位信息
     * */
    public function other()
    {

        if($this->request->isAjax()){

            $limit = $this->request->param('limit/d', 10);
            $page = $this->request->param('page/d', 10);

            $where[] = ['type','=','3'];
            $where[] = ['status','=','1'];

            $phone = input('phone');
            if($phone){
                $where[] = ['phone','=',$phone];
            }
            $title = input('title');
            if($title){
                $where[] = ['title','like','%'.$title.'%'];
            }
            $company = input('company');
            if($company){
                $where[] = ['company','like','%'.$company.'%'];
            }
            $time = input('time');
            if($time){
                $start_time = substr($time, 0, 20);//订单那创建开始时间
                $end_time = substr($time, 21, 20);//订单那创建开始时间
                $time = strtotime($start_time);
                $times = strtotime($end_time);
                $where[] = ['create_time','between',$time.','.$times];
            }

            $order = "id DESC";
            $list = Db::name('work_list')->where($where)
                ->page($page,$limit)
                ->order($order)
                ->withAttr('create_time', function ($value, $data) {
                    if($value){
                        return date('Y-m-d H:i:s', $value);
                    }else{
                        return "--";
                    }
                })
                ->select();
            $total = Db::name('work_list')->where($where)->count();

            $result = array("code" => 0, "count" => $total, "data" => $list);
            return json($result);

        }
        return $this->fetch();

    }


    /**
     * 用户申请列表
     * */
    public function applyList()
    {

        if($this->request->isAjax()){

            $limit = $this->request->param('limit/d', 10);
            $page = $this->request->param('page/d', 10);

            $where = [];

            $order = "apply_work.create_time desc";

            $phone = input('phone');
            if($phone){
                $where[] = ['apply_work.phone','=',$phone];
            }
            $title = input('title');
            if($title){
                $where[] = ['work_list.title','like','%'.$title.'%'];
            }
            $company = input('company');
            if($company){
                $where[] = ['work_list.company','like','%'.$company.'%'];
            }
            $time = input('time');
            if($time){
                $start_time = substr($time, 0, 20);//订单那创建开始时间
                $end_time = substr($time, 21, 20);//订单那创建开始时间
                $time = strtotime($start_time);
                $times = strtotime($end_time);
                $where[] = ['create_time','between',$time.','.$times];
            }

            $list = Db::view('apply_work','*')
                ->view('work_list','title,company,type','apply_work.work_id = work_list.id')
                ->where($where)
                ->page($page,$limit)
                ->order($order)
                ->withAttr('create_time', function ($value, $data) {
                    if($value){
                        return date('Y-m-d H:i:s', $value);
                    }else{
                        return "--";
                    }
                })
                ->withAttr('type', function ($value, $data) {
                    if($value == 1){
                        return "人力资源市场";
                    }else if($value == 2){
                        return "青年/妇女创业";
                    }else if($value == 3){
                        return "其他岗位信息";
                    }
                })
                ->select();

            $total = Db::view('apply_work','*')
                ->view('work_list','title,company,type','apply_work.work_id = work_list.id')
                ->where($where)->count();

            $result = array("code" => 0, "count" => $total, "data" => $list);
            return json($result);

        }
        return $this->fetch();

    }

    /**
     * 人力资源工作管理
     * */
    public function edits()
    {

        if($this->request->isPost()){

            $data = input('post.');
            if(!empty($data['workId'])){
                $update = 1;
                $id = $data['workId'];
            }else{
                $update = 0;
            }
            unset($data['workId']);

            $data['status'] = 1;
            if($update == 1){
                $res = Db::name('work_list')->where(['id' => $id])->update($data);
            }else{
                $res = Db::name('work_list')->insert($data);
            }

            if($res){
                $this->success('操作成功');
            }else{
                $this->error('操作失败');
            }

        }else{
            $id = $this->request->param('id/d');

            if($id){
                $data = Db::name('work_list')->where(['id' => $id])->find();
            }else{
                $data = [
                    'id' => '',
                    'title' => '',
                    'wages' => '',
                    'human_num' => '',
                    'age' => '',
                    'sex' => '',
                    'education' => '',
                    'condition' => '',
                    'company' => '',
                    'contacts' => '',
                    'phone' => '',
                    'address' => '',
                    'sort' => '',
                    'type' => '',
                    'info' => ''
                ];
            }

            $this->assign('data',$data);

            return $this->fetch();
        }

    }


    /**
     * 人力工作岗位导入
     * */
    public function excelIn()
    {

        $file = request()->file('excel');

        $save_path = ROOT_PATH . 'uploads/';
        $info = $file->move($save_path);
        //print_r($info);exit;

        $filename=$save_path . DIRECTORY_SEPARATOR . $info->getSaveName();
        if(file_exists($filename)) {//如果文件存在
            if( strstr($filename,'.xlsx')) {
                $PHPReader = new \PHPExcel_Reader_Excel2007();
            }
            else {
                $PHPReader = new \PHPExcel_Reader_Excel5();
            }

            //载入excel文件
            $PHPExcel = $PHPReader->load($filename);
            $sheet = $PHPExcel->getActiveSheet(0);//获得 sheet
            $highestRow = $sheet->getHighestRow(); // 取得共有数据数
            $data=$sheet->toArray();

            $new_datas = [];
            for($i=1;$i<$highestRow;$i++){
                array_push($new_datas,$data[$i]);
            }

            if(count($new_datas)>0){
                $dataInserts = [];

                foreach ($new_datas as $k => $v){

                    $map[] = ['company','=',$v[0]];
                    $map[] = ['title','=',$v[4]];

                    $count = Db::name('work_list')->where($map)->count();
                    if(!$count){

                        $info = '<p style="box-sizing: border-box; outline: 0px; margin-top: 0px; margin-bottom: 0px; padding: 0px; color: rgb(51, 51, 51); font-family: shsc-r, &quot;Microsoft Yahei&quot;, sans-serif; font-size: 13px; letter-spacing: 1.12px; white-space: normal; background-color: rgb(255, 255, 255);"><strong style="box-sizing: border-box; outline: 0px;">招工人数:</strong>'.$v[5].'</p><p style="box-sizing: border-box; outline: 0px; margin-top: 0px; margin-bottom: 0px; padding: 0px; color: rgb(51, 51, 51); font-family: shsc-r, &quot;Microsoft Yahei&quot;, sans-serif; font-size: 13px; letter-spacing: 1.12px; white-space: normal; background-color: rgb(255, 255, 255);"><strong style="box-sizing: border-box; outline: 0px;">年龄要求:</strong>'.$v[6].'</p><p style="box-sizing: border-box; outline: 0px; margin-top: 0px; margin-bottom: 0px; padding: 0px; color: rgb(51, 51, 51); font-family: shsc-r, &quot;Microsoft Yahei&quot;, sans-serif; font-size: 13px; letter-spacing: 1.12px; white-space: normal; background-color: rgb(255, 255, 255);"><strong style="box-sizing: border-box; outline: 0px;">性别要求：</strong>'.$v[7].'<br/></p><p style="box-sizing: border-box; outline: 0px; margin-top: 0px; margin-bottom: 0px; padding: 0px; color: rgb(51, 51, 51); font-family: shsc-r, &quot;Microsoft Yahei&quot;, sans-serif; font-size: 13px; letter-spacing: 1.12px; white-space: normal; background-color: rgb(255, 255, 255);"><strong style="box-sizing: border-box; outline: 0px;">文化程度：</strong>'.$v[9].'</p><p style="box-sizing: border-box; outline: 0px; margin-top: 0px; margin-bottom: 0px; padding: 0px; color: rgb(51, 51, 51); font-family: shsc-r, &quot;Microsoft Yahei&quot;, sans-serif; font-size: 13px; letter-spacing: 1.12px; white-space: normal; background-color: rgb(255, 255, 255);"><strong style="box-sizing: border-box; outline: 0px;">条件：</strong>'.$v[10].'</p><p style="box-sizing: border-box; outline: 0px; margin-top: 0px; margin-bottom: 0px; padding: 0px; color: rgb(51, 51, 51); font-family: shsc-r, &quot;Microsoft Yahei&quot;, sans-serif; font-size: 13px; letter-spacing: 1.12px; white-space: normal; background-color: rgb(255, 255, 255);"><br/></p><p style="box-sizing: border-box; outline: 0px; margin-top: 0px; margin-bottom: 0px; padding: 0px; color: rgb(51, 51, 51); font-family: shsc-r, &quot;Microsoft Yahei&quot;, sans-serif; font-size: 13px; letter-spacing: 1.12px; white-space: normal; background-color: rgb(255, 255, 255);"><strong style="box-sizing: border-box; outline: 0px;">'.$v[0].'</strong></p><p style="box-sizing: border-box; outline: 0px; margin-top: 0px; margin-bottom: 0px; padding: 0px; color: rgb(51, 51, 51); font-family: shsc-r, &quot;Microsoft Yahei&quot;, sans-serif; font-size: 13px; letter-spacing: 1.12px; white-space: normal; background-color: rgb(255, 255, 255);">'.$v[1].'</p><p style="box-sizing: border-box; outline: 0px; margin-top: 0px; margin-bottom: 0px; padding: 0px; color: rgb(51, 51, 51); font-family: shsc-r, &quot;Microsoft Yahei&quot;, sans-serif; font-size: 13px; letter-spacing: 1.12px; white-space: normal; background-color: rgb(255, 255, 255);">'.$v[2].'</p><p style="box-sizing: border-box; outline: 0px; margin-top: 0px; margin-bottom: 0px; padding: 0px; color: rgb(51, 51, 51); font-family: shsc-r, &quot;Microsoft Yahei&quot;, sans-serif; font-size: 13px; letter-spacing: 1.12px; white-space: normal; background-color: rgb(255, 255, 255);">'.$v[3].'</p>';

                        $dataInsert = array(
                            'company' => $v[0],
                            'contacts' => $v[1],
                            'phone' => $v[2],
                            'address' => $v[3],
                            'title' => $v[4],
                            'human_num' => $v[5],
                            'age' => $v[6],
                            'sex' => $v[7],
                            'wages' => $v[8],
                            'education' => $v[9],
                            'condition' => $v[10],
                            'type' => 1,
                            'info' => $info,
                            'create_time' => time(),
                            'status' =>1
                        );

                        array_push($dataInserts,$dataInsert);
                    }

                }

                $res = Db::name('work_list')->insertAll($dataInserts, true);
                if($res){
                    $this->success('上传成功' . count($dataInserts) . '条数据');
                }else{
                    $this->error('上传失败');
                }
            }else{
                $this->success('内容为空');
            }


            // return $ret;
        }else{
            return array("resultcode" => -5, "resultmsg" => "文件不存在", "data" => null);
        }


    }


    /**
     * 其他工作岗位导入
     * */
    public function excelIns()
    {

        $file = request()->file('excel');

        $save_path = ROOT_PATH . 'uploads/';
        $info = $file->move($save_path);
        //print_r($info);exit;

        $filename=$save_path . DIRECTORY_SEPARATOR . $info->getSaveName();
        if(file_exists($filename)) {//如果文件存在
            if( strstr($filename,'.xlsx')) {
                $PHPReader = new \PHPExcel_Reader_Excel2007();
            }
            else {
                $PHPReader = new \PHPExcel_Reader_Excel5();
            }

            //载入excel文件
            $PHPExcel = $PHPReader->load($filename);
            $sheet = $PHPExcel->getActiveSheet(0);//获得 sheet
            $highestRow = $sheet->getHighestRow(); // 取得共有数据数
            $data=$sheet->toArray();

            $new_datas = [];
            for($i=1;$i<$highestRow;$i++){
                array_push($new_datas,$data[$i]);
            }

            if(count($new_datas)>0){
                $dataInserts = [];

                foreach ($new_datas as $k => $v){

                    $map[] = ['company','=',$v[0]];
                    $map[] = ['title','=',$v[4]];

                    $count = Db::name('work_list')->where($map)->count();
                    if(!$count){

                        $info = '<p style="box-sizing: border-box; outline: 0px; margin-top: 0px; margin-bottom: 0px; padding: 0px; color: rgb(51, 51, 51); font-family: shsc-r, &quot;Microsoft Yahei&quot;, sans-serif; font-size: 13px; letter-spacing: 1.12px; white-space: normal; background-color: rgb(255, 255, 255);"><strong style="box-sizing: border-box; outline: 0px;">招工人数:</strong>'.$v[5].'</p><p style="box-sizing: border-box; outline: 0px; margin-top: 0px; margin-bottom: 0px; padding: 0px; color: rgb(51, 51, 51); font-family: shsc-r, &quot;Microsoft Yahei&quot;, sans-serif; font-size: 13px; letter-spacing: 1.12px; white-space: normal; background-color: rgb(255, 255, 255);"><strong style="box-sizing: border-box; outline: 0px;">年龄要求:</strong>'.$v[6].'</p><p style="box-sizing: border-box; outline: 0px; margin-top: 0px; margin-bottom: 0px; padding: 0px; color: rgb(51, 51, 51); font-family: shsc-r, &quot;Microsoft Yahei&quot;, sans-serif; font-size: 13px; letter-spacing: 1.12px; white-space: normal; background-color: rgb(255, 255, 255);"><strong style="box-sizing: border-box; outline: 0px;">性别要求：</strong>'.$v[7].'<br/></p><p style="box-sizing: border-box; outline: 0px; margin-top: 0px; margin-bottom: 0px; padding: 0px; color: rgb(51, 51, 51); font-family: shsc-r, &quot;Microsoft Yahei&quot;, sans-serif; font-size: 13px; letter-spacing: 1.12px; white-space: normal; background-color: rgb(255, 255, 255);"><strong style="box-sizing: border-box; outline: 0px;">文化程度：</strong>'.$v[9].'</p><p style="box-sizing: border-box; outline: 0px; margin-top: 0px; margin-bottom: 0px; padding: 0px; color: rgb(51, 51, 51); font-family: shsc-r, &quot;Microsoft Yahei&quot;, sans-serif; font-size: 13px; letter-spacing: 1.12px; white-space: normal; background-color: rgb(255, 255, 255);"><strong style="box-sizing: border-box; outline: 0px;">条件：</strong>'.$v[10].'</p><p style="box-sizing: border-box; outline: 0px; margin-top: 0px; margin-bottom: 0px; padding: 0px; color: rgb(51, 51, 51); font-family: shsc-r, &quot;Microsoft Yahei&quot;, sans-serif; font-size: 13px; letter-spacing: 1.12px; white-space: normal; background-color: rgb(255, 255, 255);"><br/></p><p style="box-sizing: border-box; outline: 0px; margin-top: 0px; margin-bottom: 0px; padding: 0px; color: rgb(51, 51, 51); font-family: shsc-r, &quot;Microsoft Yahei&quot;, sans-serif; font-size: 13px; letter-spacing: 1.12px; white-space: normal; background-color: rgb(255, 255, 255);"><strong style="box-sizing: border-box; outline: 0px;">'.$v[0].'</strong></p><p style="box-sizing: border-box; outline: 0px; margin-top: 0px; margin-bottom: 0px; padding: 0px; color: rgb(51, 51, 51); font-family: shsc-r, &quot;Microsoft Yahei&quot;, sans-serif; font-size: 13px; letter-spacing: 1.12px; white-space: normal; background-color: rgb(255, 255, 255);">'.$v[1].'</p><p style="box-sizing: border-box; outline: 0px; margin-top: 0px; margin-bottom: 0px; padding: 0px; color: rgb(51, 51, 51); font-family: shsc-r, &quot;Microsoft Yahei&quot;, sans-serif; font-size: 13px; letter-spacing: 1.12px; white-space: normal; background-color: rgb(255, 255, 255);">'.$v[2].'</p><p style="box-sizing: border-box; outline: 0px; margin-top: 0px; margin-bottom: 0px; padding: 0px; color: rgb(51, 51, 51); font-family: shsc-r, &quot;Microsoft Yahei&quot;, sans-serif; font-size: 13px; letter-spacing: 1.12px; white-space: normal; background-color: rgb(255, 255, 255);">'.$v[3].'</p>';

                        $dataInsert = array(
                            'company' => $v[0],
                            'contacts' => $v[1],
                            'phone' => $v[2],
                            'address' => $v[3],
                            'title' => $v[4],
                            'human_num' => $v[5],
                            'age' => $v[6],
                            'sex' => $v[7],
                            'wages' => $v[8],
                            'education' => $v[9],
                            'condition' => $v[10],
                            'type' => 3,
                            'info' => $info,
                            'create_time' => time(),
                            'status' =>1
                        );

                        array_push($dataInserts,$dataInsert);
                    }

                }

                $res = Db::name('work_list')->insertAll($dataInserts, true);
                if($res){
                    $this->success('上传成功' . count($dataInserts) . '条数据');
                }else{
                    $this->error('上传失败');
                }
            }else{
                $this->success('内容为空');
            }


            // return $ret;
        }else{
            return array("resultcode" => -5, "resultmsg" => "文件不存在", "data" => null);
        }


    }

}